package com.jiuyin.dianjing.ui.fragment.match;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.recyclerview.widget.DefaultItemAnimator;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.decoration.CustomDividerItemDecoration;
import com.jiuyin.dianjing.model.Compete;
import com.jiuyin.dianjing.ui.activity.matchInfo.MatchInfoDetailActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseRefreshLazyFragment;
import com.jiuyin.dianjing.util.LogUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMPETELIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMPET_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_GAMEID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_IS_CREATOR;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_NAME;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_PAGE_NUM;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.OSS_IMG_SCREEN_TOPIC;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_NUM;

/**
 * 赛事信息
 */
public class FragmentMatch extends BaseRefreshLazyFragment<Compete> {
    private String gameId;
    private static final String ARG_PARAM1 = "param1";
    private boolean mAllMatch;//是否是全部比赛

    public FragmentMatch() {
        super(PAGE_NUM);
    }

    public static FragmentMatch newInstance(String gameId, boolean allMatch) {
        FragmentMatch fragmentGame = new FragmentMatch();
        Bundle args = new Bundle();
        args.putString(KEY_GAMEID, gameId);
        args.putBoolean(ARG_PARAM1, allMatch);
        fragmentGame.setArguments(args);
        return fragmentGame;
    }

    @Override
    public void fetchData() {
        HashMap<String, Object> data = new HashMap<>();
        data.put(KEY_TOKEN, HelperApplication.getToken());
        data.put(KEY_GAMEID, gameId);
        data.put(KEY_PAGE_NUM, mPageNum);
        JSONObject jsonObject = new JSONObject(data);
        ApiResultCallback request = new ApiResultCallback() {
            @Override
            public void onSuccess(JsonObject data) {
                LogUtil.log("FragmentMatch onSuccess " + data.toString());
                if (null != data && data.has(KEY_COMPETELIST)) {
                    List<Compete> competeList = new Gson().fromJson(data.getAsJsonArray(KEY_COMPETELIST), new TypeToken<List<Compete>>() {
                    }.getType());
                    dealWithSuccessInfo(competeList);
                } else {
                    dealOtherInfo();
                }
            }

            @Override
            public void onFail(String msg) {
                dealOtherInfo();
            }

            @Override
            public void onError(String msg) {
                dealOtherInfo();
            }
        };
        ServerApi.post(mAllMatch ? ApiEnum.APP_GETCOMPETITIONALLCOURSE.getUrl() : ApiEnum.APP_SEARCHJOIN_COMPETITION_API.getUrl(), jsonObject, null,
                null, d -> addDisposable(d), request);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_match;
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            gameId = getArguments().getString(KEY_GAMEID);
            mAllMatch = getArguments().getBoolean(ARG_PARAM1);
        }
        mAdapter = new BaseRecyclerAdapter<Compete>(R.layout.item_info_match) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, Compete model, int position) {
                if (null != model) {
                    holder.text(R.id.tv_match_title, model.name);
                    holder.text(R.id.tv_match_name, model.competename);
                    holder.text(R.id.tv_match_time, model.time);
                    holder.text(R.id.tv_match_status, model.course);
                    ImageView ivGame = holder.itemView.findViewById(R.id.img_match);
                    if (model != null && model.logo != null) {
                        StringBuilder url = new StringBuilder(model.logo);
                        if (url != null) {
                            url.append(OSS_IMG_SCREEN_TOPIC);
                            Glide.with(getContext())
                                    .load(url.toString())
                                    .into(ivGame);
                        }
                    }
                }
            }
        };
        mAdapter.setOnItemClickListener((parent, view, position, id) -> {
            Compete model = mAdapter.getItem(position);
            Intent intent = new Intent(getActivity(), MatchInfoDetailActivity.class);
            intent.putExtra(KEY_COMPET_ID, model.compet_id);
            intent.putExtra(KEY_NAME, model.name);
            intent.putExtra(KEY_IS_CREATOR, model.isCreator);
            startActivity(intent);
        });
        mRcvPage.addItemDecoration(new CustomDividerItemDecoration(getContext()));//添加分割线
        mRcvPage.setItemAnimator(new DefaultItemAnimator());
        mRcvPage.setAdapter(mAdapter);
    }

    @Override
    protected void initView() {
    }
}
